<script setup lang="ts">
import {computed, reactive} from "vue";

const author = reactive({
  name: 'John Doe',
  books: [
    'Vue 2 - Advanced Guide',
    'Vue 3 - Basic Guide',
    'Vue 4 - The Mystery'
  ]
})

// 计算属性
const publishedBooksMessage = computed(() => {
  return author.books.length > 0 ? `${author.name} has published ${author.books.length} books` : `${author.name} has not published any books`
})
</script>

<template>
  <div>
    <h1>计算属性</h1>
    <p>
      模板中的表达式虽然方便，但也只能用来做简单的操作。<br/>
      如果在模板中写太多逻辑，会让模板变得臃肿，难以维护。<br/>
      一个计算属性仅会在其响应式依赖更新时才重新计算。<br/>
      计算属性可以帮助我们将模板中的逻辑和数据分离，使模板更加清晰。<br/>
    </p>
    <div>
      <p>Has published books:</p>
      <span>{{ author.books.length > 0 ? 'Yes' : 'No' }}</span>
    </div>
    <div>
      <p>Has published books:</p>
      <span>{{ publishedBooksMessage }}</span>
    </div>
  </div>
</template>

<style scoped>

</style>